package middle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.PriorityQueue;
import java.util.stream.Collectors;

public class Solution_2034 {

    private Integer currentTime = Integer.MIN_VALUE;
    private Map<Integer, Integer> map;
    private final PriorityQueue<int[]> priorityQueueAsc;
    private final PriorityQueue<int[]> priorityQueueDesc;

    public Solution_2034() {
        map = new HashMap<>();
        priorityQueueAsc = new PriorityQueue<>(Comparator.comparingInt(e -> e[0]));
        priorityQueueDesc = new PriorityQueue<>((e1, e2) -> e2[0] - e1[0]);
    }

    public void update(int timestamp, int price) {
        priorityQueueAsc.add(new int[]{price, timestamp});
        priorityQueueDesc.add(new int[]{price, timestamp});
        map.put(timestamp, price);
        currentTime = Math.max(currentTime, timestamp);
    }

    public int current() {
        return map.get(currentTime);
    }

    public int maximum() {
        if (priorityQueueDesc.isEmpty()) {
            return 0;
        }
        do {
            int[] peek = priorityQueueDesc.peek();
            Integer price = map.get(peek[1]);
            if (peek[0] == price) {
                return price;
            } else {
                priorityQueueDesc.poll();
            }
        } while (!priorityQueueDesc.isEmpty());
        return 0;
    }

    public int minimum() {
        if (priorityQueueAsc.isEmpty()) {
            return 0;
        }
        do {
            int[] peek = priorityQueueAsc.peek();
            Integer price = map.get(peek[1]);
            if (peek[0] == price) {
                return price;
            } else {
                priorityQueueAsc.poll();
            }
        } while (!priorityQueueAsc.isEmpty());
        return 0;
    }

    public static void main(String[] args) {
//        Solution_2034 model = new Solution_2034();
//        model.update(1, 10);
//        model.update(2, 5);
//        System.out.println(model.current());
//        System.out.println(model.maximum());
//        model.update(1, 3);
//        System.out.println(model.maximum());
//        model.update(4, 2);
//        System.out.println(model.minimum());
        List<Long> list = new ArrayList<>();
        list.add(null);
        Optional<Long> first = list.stream().filter(Objects::nonNull).findFirst();
        System.out.println(first.isPresent());
    }
}
